package com.intct.hbase.util;

import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-07-10 09:40
 */
public class HBaseConnection {

    /*
        Connection creation is a heavy-weight operation. Connection implementations are thread-safe,
         so that the client can create a connection once, and share it with different threads.
         Table and Admin instances, on the other hand, are light-weight and are not thread-safe.
         Typically, a single connection per client application is instantiated and every
         thread will obtain its own Table instance. Caching or pooling of Table and Admin is not recommended.
        连接创建是一项重量级操作。连接实现是线程安全的，因此 Client 端可以创建一次连接，并与不同的线程共享它。
        Table 和 Admin 实例是轻量级的，不是线程安全的。通常，每个客户端应用程序都会实例化一个连接，
        并且每个线程都将获得自己的 Table 实例。不建议缓存或池化 Table 和 Admin。
        Admin实例：进行DDL操作
        Table实例：进行DML操作
     */

    private static Connection connection;

    static {
        try {
            connection = ConnectionFactory.createConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return
     */
    public static Connection getConnection() throws IOException {
        return connection;
    }

    /**
     * 关闭
     * @param connection
     * @throws IOException
     */
    public static void close(Connection connection) throws IOException {
        if (connection != null) {
            connection.close();
        }
    }

}
